package name.matthewgreet.example2.common; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.ValidationAware; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; /** * <P>If the action implements {@link ValidationAware}, stores the errors and messages in the session so it may be * retrieved by a later action that displays it. It is usually part of the aflUpdateStack interceptor stack and * retrieved by {@link MessageRetrieveInterceptor} as part of the aflViewStack interceptor stack.</P> * * * <H3>Interceptor parameters</H3> * <P>none</P> * * <H3>Extending the interceptor</H3> * <P>This can't be usefully extended.</P> * * <H3>Example code</H3> * <PRE> * @InterceptorRefs({ * @InterceptorRef(value="messageStore"), * @InterceptorRef(value="basicStack") *}) * </PRE> */ public class MessageStoreInterceptor extends AbstractInterceptor { public static class StoredMessages { private Collection<String> actionErrors, actionMessages; private Map<String,List<String>> fieldErrors; public StoredMessages() { actionErrors = new ArrayList<String>(); actionMessages = new ArrayList<String>(); fieldErrors = new HashMap<String,List<String>>(); } public Collection<String> getActionErrors() { return actionErrors; } public void setActionErrors(Collection<String> actionErrors) { this.actionErrors = actionErrors; } public Collection<String> getActionMessages() { return actionMessages; } public void setActionMessages(Collection<String> actionMessages) { this.actionMessages = actionMessages; } public Map<String,List<String>> getFieldErrors() { return fieldErrors; } public void setFieldErrors(Map<String,List<String>> fieldErrors) { this.fieldErrors = fieldErrors; } } private static final long serialVersionUID = -8274766657096416432L; public static final String SESSION_STORED_MESSAGES = MessageStoreInterceptor.class + "_STORED_MESSAGES"; public MessageStoreInterceptor() { // Empty } @Override public String intercept(ActionInvocation invocation) throws Exception { ValidationAware validationAware; StoredMessages storedMessages; String result; List<String> fieldErrors; Map<String,List<String>> allFieldErrors; result = invocation.invoke(); if (invocation.getAction() instanceof ValidationAware) { validationAware = (ValidationAware)invocation.getAction(); // Merge with any existing stored messages storedMessages = (StoredMessages)ActionContext.getContext().getSession().get(SESSION_STORED_MESSAGES); if (storedMessages == null) { storedMessages = new StoredMessages(); } storedMessages.getActionErrors().addAll(validationAware.getActionErrors()); storedMessages.getActionMessages().addAll(validationAware.getActionMessages()); allFieldErrors = storedMessages.getFieldErrors(); for (Entry<String,List<String>> entrySet: validationAware.getFieldErrors().entrySet()) { fieldErrors = allFieldErrors.get(entrySet.getKey()); if (fieldErrors == null) { fieldErrors = new ArrayList<String>(); } fieldErrors.addAll(entrySet.getValue()); allFieldErrors.put(entrySet.getKey(), fieldErrors); } ActionContext.getContext().getSession().put(SESSION_STORED_MESSAGES, storedMessages); } return result; } }